﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.Admin == "undefined") Axis.Admin = {};

Axis.Admin.TopMenu = {
    Init: function () {
        var thisObj = this;
        thisObj.Grid.Init();
        thisObj.EventClick();
        var cmaskdate = new CMaskDate();
        cmaskdate.Init($("input[role=datepicker]"));
        $("#ddlParentName option").each(function () {
            var text = $(this).text();
            $(this).text($.RemoveHTMLTags(text));
        });
        $('#txt_BorderColor').ColorPicker({
            onSubmit: function (hsb, hex, rgb, el) {
                $(el).val(hex);
                $(el).ColorPickerHide();
                $(el).css("background", "#" + hex);
            },
            onBeforeShow: function () {
                $(this).ColorPickerSetColor(this.value);
            }
        }).bind('keyup', function () {
            $(this).ColorPickerSetColor(this.value);
        });
    },
    Grid: {
        ParentID: 23,
        ID: "#GridContents",
        PagesID: "#GridPages",
        GridURL: "Service/NavigationService.ashx?Action=GET&FunctionCode=MenuCategory",
        Init: function () {
            var thisObj = this;
            var options = {
                url: thisObj.GridURL,
                datatype: "json",
                colNames: [
                    'ID', 'Code', 'Name', 'Parent Name', 'ParentID', 'Description', 'ZOrder', 'ActiveImage', 'DeActiveImage', 'Image', 'BorderColor', 'Action'
                ],
                colModel: [
                    { name: 'ID', index: 'ID', width: 50, frozen: true },
                    { name: 'Code', index: 'Code', width: 100, frozen: true },
                    { name: 'Name', index: 'Name', width: 150, frozen: true },
                    { name: 'ParentName', index: 'ParentName', width: 150, formatter: function (cellvalue, options, rowObject) { return $.RemoveHTMLTags(cellvalue); } },
                    { name: 'ParentID', index: 'ParentID', width: 50, hidden: true },
                    { name: 'Description', index: 'Description', width: 250 },
                    { name: 'ZOrder', index: 'ZOrder', width: 50 },
                    { name: 'ActiveImage', index: 'ActiveImage', width: 50 },
                    { name: 'DeActiveImage', index: 'DeActiveImage', width: 50 },
                    { name: 'Image', index: 'Image', width: 50 },
                    { name: 'BorderColor', index: 'BorderColor', width: 50, formatter: function (cellvalue, options, rowObject) { console.log(options); return cellvalue; } },
                    { name: 'Action', index: 'Action', width: 90, align: 'center' }
                ],
                afterInsertRow: function (rowid, aData, addData) {
                    var icon = '<img class="imgAction" src="../../Images/16x16/edit.png" onclick="ShowEdit(' + rowid + ')"/>';
                    $(thisObj.ID).setCell(rowid, 'Action', icon, {});

                },
                onCellSelect: function (rowid, iCol, cellcontent, e) {

                },
                onSelectRow: function (rowid, status, e) {

                },
                ondblClickRow: function (rowid, iRow, iCol, e) {

                },
                gridComplete: function () {
                    $('.cell_image').tooltip({
                        delay: 0,
                        track: true,
                        showURL: false,
                        bodyHandler: function () {
                            return $("<img/>").attr("src", this.src);
                        }
                    });
                },
                multiselect: true,
                height: 'auto',
                rownumbers: true,
                autowidth: true,
                //forceFit: true,
                shrinkToFit: false,
                rowNum: 10,
                pager: thisObj.PagesID
            };
            thisObj.GridObj = new CGrid(options);
            thisObj.GridObj.Init(thisObj.ID);
            thisObj.GridObj.NavGrid();
            thisObj.GridObj.FrozenColumns();
        },
        Reload: function () {
            var thisObj = this;
            if (thisObj.GridObj)
                thisObj.GridObj.ReloadGrid();
        },
        SelectRow: function () {
            var thisObj = this;
            if (thisObj.GridObj) {
                return thisObj.GridObj.SelectRow();
            }
            return null;
        }
    },
    DialogEdit: {
        ID: "#FormEdit",
        Dialog: null,
        Init: function (action, opts) {
            var thisObj = this;
            thisObj.ClearForm();

            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
            else {
                thisObj.Dialog = new CDialog({
                    title: "Edit User",
                    width: 430
                });
                thisObj.Dialog.Init(thisObj.ID);
            }
            var buttonText = "";
            switch (action) {
                case "INSERT":
                    thisObj.Dialog.SetTitle("Add New User");
                    $(thisObj.ID).find("[data-role='INSERT']").show();

                    $(thisObj.ID).find("#txtLoginName").focus();
                    buttonText = "Add";
                    break;
                case "UPDATE":
                    thisObj.Dialog.SetTitle("Edit: " + opts.LoginName);
                    $.each(opts, function (key, value) {
                        if (key == "ActiveImage" || key == "DeActiveImage" || key == "Image") {
                            value = $(value).attr("FileName") || "";
                        }
                        else if (key == "BorderColor") {
                            value = $(value).attr("Value") || "";
                            $(thisObj.ID).find("[data-field=" + key + "]").css("background", "#" + value);
                        }
                        $(thisObj.ID).find("[data-field=" + key + "]").val(value);
                    });
                    buttonText = "Update";
                    break;
            }
            var buttons = [
                        {
                            text: buttonText,
                            click: function (e) {
                                Axis.Admin.TopMenu.Update(action, opts);
                                thisObj.Close();
                            }
                        },
                        {
                            text: "Cancel",
                            click: function (e) {
                                thisObj.Close();
                            }
                        }

            ];
            $(thisObj.ID).dialog('option', 'buttons', buttons);
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Close();
            }
        },
        ClearForm: function () {
            $("#FormEdit").find("[data-field]").each(function () {
                var $this = $(this);
                $this.val("");
                if ($this.is("select")) {
                    var value = $this.find("option[Default-Value='1']").attr("value");
                    $this.val(value);
                }

            });
            $("#txt_BorderColor").css("background", "#FFF");
            $("#FormEdit").find("[data-role='INSERT']").hide();
        }
    },
    Update: function (type, opts) {
        var thisObj = this;
        var data = {};
        switch (type) {
            case "INSERT":
            case "UPDATE":
                $("#FormEdit").find("[data-field]").each(function () {
                    var $this = $(this);
                    if ($this.attr("data-field")) {
                        var key = $this.attr("data-field");
                        data[key] = $this.val();
                    }
                });
                break;
            case "DELETE":
                data.ID = opts.ID;
                break;
        }
       
        data.Action = type;
        data.DataCode = "MenuCategory";

        $.Ajax({
            url: 'Service/NavigationService.ashx',
            type: 'POST',
            data: data,
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    data = eval("(" + data + ")");
                    if (data.IsSuccessfull) {
                        thisObj.Grid.Reload();
                    }
                }
            },
            beforeSend: function (jqXHR, settings) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });
    },
    DialogFilter: {
        Init: function () { }
    },
    EventClick: function () {
        var thisObj = this;
        $("#imgUserAdd").click(function () {
            thisObj.DialogEdit.Init("INSERT");
        });
        $("#imgUserEdit").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID)
                thisObj.DialogEdit.Init("UPDATE", rowData);
            else alert("Please Select Row");
        });
        $("#imgUserDel").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID) {
                $.CAlert.Confirm({
                    Title: "Xóa danh mục",
                    Content: $.string.Format("Bạn có chắc muốn xóa danh mục: [{0}].", rowData.Name),
                }, function (isOK) {
                    if (isOK)
                        thisObj.Update("DELETE", rowData);
                });
            }
            else {
                $.CAlert.Alert({
                    Title: "Xóa",
                    Content: "Vui lòng chọn quà tặng cần xóa.",
                });
            }

        });
        $("#imgUserFilter").click(function () {
            thisObj.DialogFilter.Init();
        });
        $("#btnActiveImage").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            //finder.startupPath = "Images:/Project/";
            finder.selectActionFunction = function (fileUrl) {
                $('#txt_ActiveImage').val(fileUrl);
            };
            finder.popup();
        });
        $("#btnDeActiveImage").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            finder.selectActionFunction = function (fileUrl) {
                $('#txt_DeActiveImage').val(fileUrl);
            };
            finder.popup();
        });
        $("#btnImage").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            finder.selectActionFunction = function (fileUrl) {
                $('#txt_Image').val(fileUrl);
            };
            finder.popup();
        });
    }
};
$(function () {
    Axis.Admin.TopMenu.Init();
});
function ShowEdit(rowID) {
    var thisObj = Axis.Admin.TopMenu;
    var rowData = $(thisObj.Grid.ID).jqGrid('getRowData', rowID);
    if (rowData && rowData.ID)
        thisObj.DialogEdit.Init("UPDATE", rowData);
}